package com.qfedu.openplateform.manager.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.openplateform.manager.entity.AjaxMessage;
import com.qfedu.openplateform.manager.entity.Customer;
import com.qfedu.openplateform.manager.entity.TableData;
import com.qfedu.openplateform.manager.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Mr.Li
 * @date 2021/10/27 16:38
 * @className CustomerController
 * @description 用户管理模块主控制层
 */

@RestController
@RequestMapping("/sys/customer")
public class CustomerController {


    @Autowired
    private CustomerService customerService;

    /**
     * 分页 并进行 查询所有用户
     */
    @RequestMapping("table")
    public TableData findByPage(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "limit", required = false, defaultValue = "20") Integer pageSize,
            String name,
            Integer state
    ) {

        System.out.println(name + state);

        Customer customer = new Customer();
        customer.setUsername(name);
        customer.setState(state);
        System.out.println("customer = " + customer);

        TableData<Customer> customerTableData = new TableData<>();

        // 设置分页参数
        PageHelper.startPage(pageNum, pageSize);

        // 查询所有客户
        List<Customer> customerList = customerService.findAll(customer);

        PageInfo<Customer> customerPageInfo = new PageInfo<>(customerList);

        customerTableData.setData(customerPageInfo.getList());

        customerTableData.setCount(customerPageInfo.getTotal());

        return customerTableData;
    }


//    /**
//     * 分页 并进行 查询所有用户
//     */
    @RequestMapping("tree")
    public List<Customer> findAll(Customer customer) {
        // 查询所有客户
        return customerService.findAll(customer);
    }

    // 添加客户信息
    @RequestMapping("add")
    public AjaxMessage add(Customer customer) {
        try {
            customerService.add(customer);
            return new AjaxMessage(true, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxMessage(false, "添加失败");
    }


    // 更新客户信息
    @RequestMapping("update")
    public AjaxMessage update(Customer customer) {
        System.out.println("customer = " + customer);
        try {
            customerService.update(customer);
            return new AjaxMessage(true, "修改成功1");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxMessage(false, "修改失败");
    }

    // 删除客户
    @RequestMapping("del")
    public AjaxMessage del(int[] ids) {
        try {
            customerService.del(ids);
            return new AjaxMessage(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxMessage(false, "删除失败");
    }


}
